/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator.Dialog;

import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Window;
import ohos.app.Context;

/**
 * Dialog封装类
 */
public class NiceDialog extends CommonDialog {
    /**
     * 上下文
     */
    private final Context myContext;
    /**
     * The Layout.
     */
    private int layout;
    /**
     * The Type.
     */
    private int type;
    /**
     * The Is enabled.
     */
    private boolean isEnabled;
    /**
     * The View convert listener.
     */
    private ViewConvertListener viewConvertListener;

    /**
     * Instantiates a new Nice dialog.
     *
     * @param context the context
     */
    private NiceDialog(Context context) {
        super(context);
        this.myContext = context;
    }

    /**
     * 初始化dialog
     *
     * @param context 上下文
     * @return NiceDialog 自定义Dialog
     */
    public static NiceDialog init(Context context) {
        return new NiceDialog(context);
    }

    /**
     * Covert holder view holder.
     *
     * @param view    the view
     * @param context the context
     * @return the view holder
     */
    private static ViewHolder covertHolder(Component view, Context context) {
        ViewHolder viewHolder = ViewHolder.create(view, context);
        return viewHolder;
    }

    /**
     * 设置gravity
     *
     * @param gravity ConstandCodes常量
     * @return NiceDialog 自定义Dialog
     */
    public NiceDialog setGravity(int gravity) {
        switch (gravity) {
            case ConstandCodes.TOP:
                break;
            case ConstandCodes.BOTTOM:
                Window window = this.getWindow();
                window.setLayoutAlignment(LayoutAlignment.BOTTOM);
                break;
            case ConstandCodes.LEFT:
                break;
            case ConstandCodes.RIGHT:
                break;
            case ConstandCodes.CENTER:
                break;
        }
        return this;
    }

    /**
     * 设置Dialog布局接口
     *
     * @param viewConvertListener Dialog布局对外暴露接口
     * @return NiceDialog 自定义Dialog
     */
    public NiceDialog setViewConvertListener(ViewConvertListener viewConvertListener) {
        this.viewConvertListener = viewConvertListener;
        return this;
    }

    /**
     * 设置layout资源文件
     *
     * @param layout_share_layout 资源ID
     * @param type                dialog类型
     * @return NiceDialog 自定义Dialog
     */
    public NiceDialog setLayoutId(int layout_share_layout, int type) {
        this.layout = layout_share_layout;
        this.type = type;
        return this;
    }

    /**
     * On create.
     */
    @Override
    protected void onCreate() {
        super.onCreate();
        Component shareLayout = LayoutScatter.getInstance(this.myContext)
                .parse(this.layout, null, true);
        ViewHolder viewHolder = NiceDialog.covertHolder(shareLayout, this.myContext);
        if (this.viewConvertListener != null) this.viewConvertListener.convertView(viewHolder, this);
        if (this.type == ConstandCodes.CONFRIMTYPE) {
            int height = shareLayout.getLayoutConfig().height;
            this.setSize(800, height);
        }
        this.setDialogListener(new DialogListener() {
            @Override
            public boolean isTouchOutside() {
                if (NiceDialog.this.isEnabled) NiceDialog.this.destroy();
                return true;
            }
        });
        this.setContentCustomComponent(shareLayout);
    }

    /**
     * 设置是否点击外部关闭dialog
     *
     * @param isEnabled false为不关闭 true为关闭
     * @return NiceDialog 自定义Dialog
     */
    public NiceDialog setSwipeToClose(boolean isEnabled) {
        this.isEnabled = isEnabled;
        return this;
    }
}

